package com.yf.catcafe.controller;

import com.yf.catcafe.pojo.dto.CatDto;
import com.yf.catcafe.pojo.enity.Cat;
import com.yf.catcafe.pojo.vo.QueryVo;
import com.yf.catcafe.result.Result;
import com.yf.catcafe.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/catcafe")
@CrossOrigin(origins = "*")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    private static final String UPLOAD_DIR = "D:/CatCafeUploads/";

    @GetMapping("/query")
    public Result<List<QueryVo>> query(@RequestParam(value = "query",required = false) String name,
                                      @RequestParam(value = "category",required = false) String category){
        List<Cat> cats=goodsService.query(name,category);
        List<QueryVo> queryVo = cats.stream().map(cat -> {
            QueryVo vo = new QueryVo();
            vo.setName(cat.getName());
            vo.setPrice(cat.getPrice());
            vo.setImage(cat.getImage());
            vo.setCategory(cat.getCategory());
            vo.setDescription(cat.getDescription());
            vo.setId(cat.getId());
            return vo;
        }).collect(Collectors.toList());
       return Result.success(queryVo);
    }

    @GetMapping("/query/{catId}")
    public Result<QueryVo> queryOne(@PathVariable(value = "catId") Long id){
        Cat cat=goodsService.getById(id);
        QueryVo queryVo=new QueryVo();
        queryVo.setDescription(cat.getDescription());
        queryVo.setName(cat.getName());
        queryVo.setPrice(cat.getPrice());
        queryVo.setCategory(cat.getCategory());
        queryVo.setImage(cat.getImage());
        queryVo.setId(cat.getId());
        return Result.success(queryVo);
    }

    @GetMapping("/byName")
    public Result<QueryVo> queryByName(@RequestParam String name){
        Cat cat=goodsService.queryByName(name);
        QueryVo queryVo=new QueryVo();
        queryVo.setDescription(cat.getDescription());
        queryVo.setName(cat.getName());
        queryVo.setPrice(cat.getPrice());
        queryVo.setCategory(cat.getCategory());
        queryVo.setImage(cat.getImage());
        queryVo.setId(cat.getId());
        return Result.success(queryVo);
    }

    @GetMapping("/byCategory")
    public Result<List<QueryVo>> queryByCategory(@RequestParam String category){
        List<Cat> cats=goodsService.queryByCategory(category);
        List<QueryVo> queryVo = cats.stream().map(cat -> {
            QueryVo vo = new QueryVo();
            vo.setName(cat.getName());
            vo.setPrice(cat.getPrice());
            vo.setImage(cat.getImage());
            vo.setCategory(cat.getCategory());
            vo.setDescription(cat.getDescription());
            vo.setId(cat.getId());
            return vo;
        }).collect(Collectors.toList());
        return Result.success(queryVo);
    }

//    @PostMapping("/add")
//    public Result add(@RequestBody CatDto catDto){
//        goodsService.add(catDto);
//        return Result.success();
//    }

    @PostMapping("/add")
    public Result<String> add(
            @RequestParam("name") String name,
            @RequestParam("category") String category,
            @RequestParam("price") BigDecimal price,
            @RequestParam("description") String description,
            @RequestParam("image") MultipartFile image
    ) throws IOException {

        // 保存图片到服务器
        String fileName = UUID.randomUUID() + "_" + image.getOriginalFilename();
        Path filePath = Paths.get(UPLOAD_DIR + fileName);
        image.transferTo(filePath);

        // 构造 DTO 并调用 service 添加数据库记录
        CatDto catDto = new CatDto();
        catDto.setName(name);
        catDto.setCategory(category);
        catDto.setPrice(price);
        catDto.setDescription(description);
        catDto.setImage("/uploads/" + fileName); // 存储相对路径

        goodsService.add(catDto);

        return Result.success("添加成功");
    }

    @GetMapping("/test-image")
    public void testImage(HttpServletResponse response) throws IOException {
        String imagePath = "D:/CatCafeUploads/f92dbc98-14aa-4793-b3e0-edc01de16280_26.png";
        Path path = Paths.get(imagePath);

        if (Files.exists(path)) {
            response.setContentType("image/png");
            Files.copy(path, response.getOutputStream());
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "图片不存在");
        }
    }

    @GetMapping("/seckillcats")
    public Result<List<QueryVo>> seckillCats(){
        List<Cat> cats=goodsService.querySeckill();
        List<QueryVo> queryVos=cats.stream().map(cat -> {
            QueryVo vo=new QueryVo();
            vo.setId(cat.getId());
            vo.setPrice(cat.getPrice());
            vo.setDescription(cat.getDescription());
            vo.setImage(cat.getImage());
            vo.setName(cat.getName());
            return vo;
        }).collect(Collectors.toList());
        return Result.success(queryVos);
    }




}
